package main

import (
    "big"
    "flag"
    "fmt"
    "strconv"

    factorial "./Go/commonfact"
)


func goFactRange(start *big.Int, end *big.Int, reply chan *big.Int) {
    fact := factorial.FactRange(start, end)
    reply <- fact
}


func main() {
    flag.Parse()

    routineCount, err := strconv.Atoi64(flag.Arg(0))
    replies := make(chan *big.Int, routineCount)

    if err != nil {
        panic("Crap")
    }

    factInt64, err := strconv.Atoi64(flag.Arg(1))

    if err != nil {
        panic("Crap again!")
    }

    fact := big.NewInt(factInt64)

    step := big.NewInt(0)
    step = step.Div(fact, big.NewInt(routineCount))

    bottom := big.NewInt(1)
    for bottom.Cmp(fact) == -1 {
        top := big.NewInt(0)
        top.Add(bottom, step)
        top.Sub(top, big.NewInt(1))
        go goFactRange(bottom, top, replies)
        bottom = big.NewInt(0).Add(bottom, step)
    }

    total := big.NewInt(1)

    var x int64
    for ; x < routineCount; x++ {
        result := <-replies
        total.Mul(total, result)
    }

    fmt.Println(total)
}
